package com.qf.service.impl;

import com.qf.constant.CacheConstants;
import com.qf.constant.OperatorConstants;
import com.qf.feign.CacheFeign;
import com.qf.model.StandardSubmit;
import com.qf.service.FilterChainService;
import com.qf.util.CheckMobileOperator;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

/*
 *@Description:号段补全策略
 *@author:刘哥
 *@Date:86180 2022/10/19 17:46
 */
@Service("phase")
@Slf4j
public class PhaseServiceImpl implements FilterChainService {
    @Autowired
    private CacheFeign cacheFeign;

    @Override
    public boolean strategy(StandardSubmit standardSubmit) {
        log.info("[策略模块-号段补全策略] 开始执行==========");
//        获取目标手机号
        String destMobile = standardSubmit.getDestMobile();
//        判断目标用户是哪个运营商的
        if(CheckMobileOperator.isChinaMobilePhoneNum(destMobile)){
//            手机号是移动
            standardSubmit.setGatewayID(OperatorConstants.CHINA_MOBILE);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_MOBILE);
        }else if (CheckMobileOperator.isChinaUnicomPhoneNum(destMobile)){
//            联通
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNICOM);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNICOM);
        }else  if(CheckMobileOperator.isChinaTelecomPhoneNum(destMobile)){
//            电信
            standardSubmit.setGatewayID(OperatorConstants.CHINA_TELECOM);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_TELECOM);
        }else{
//            未知号码
            standardSubmit.setGatewayID(OperatorConstants.CHINA_UNKNOWN);
            standardSubmit.setOperatorId(OperatorConstants.CHINA_UNKNOWN);
        }
//        根据手机号(前7位)判断归属地
        String key = destMobile.substring(0, 7);
        String value = cacheFeign.get(CacheConstants.CACHE_PREFIX_PHASE + key);
        if(StringUtils.isEmpty(value)){
            log.info("[策略模块-号段补全策略] 获取归属地编码失败==========");
        }
        String[] proviceAndCity = value.split(",");
        standardSubmit.setProvinceId(Integer.valueOf(proviceAndCity[0]));
        standardSubmit.setCityId(Integer.valueOf(proviceAndCity[1]));
//        获取短信的路由数据=clientId+gatewayId
        String routerkey = CacheConstants.CACHE_PREFIX_ROUTER + standardSubmit.getClientID()+":"+standardSubmit.getGatewayID();
        Map<String,String> routerMap = cacheFeign.hMGet(routerkey);
//        获取通道数据
        String channelKey=CacheConstants.CACHE_PREFIX_SMS_CHANNEL+standardSubmit.getOperatorId()+":"+standardSubmit.getGatewayID();
        Map<String,String> channelMap = cacheFeign.hMGet(channelKey);

//      组装改短信的下发源信号也就是来电号码
        if(routerMap !=null && channelMap !=null){
//            运营通道号
            String spnumber = channelMap.get("spnumber");
//            扩展号码
            String extendnumber = routerMap.get("extendnumber");
//            下发源号码
            standardSubmit.setSrcNumber(spnumber+extendnumber);
        }
//        获取短信费用
        String price = routerMap.get("price");
        standardSubmit.setFee(Long.valueOf(price));

        log.info("[策略模块-号段补全策略] 执行完毕==========");
        return true;
    }
}
